package com.venutech.gongan.api.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.venutech.gongan.service.AuditLogService;
import com.venutech.gongan.service.impl.AuditLogServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.concurrent.*;

@Configuration
public class ServerConfiguration {

    @Value("${RZLOG_XTID}")
    private  String xtid;
    //终端地址(IP或移动终端mac)
    @Value("${RZLOG_ZDDZ}")
    private String zddz;
    @Value("${RZLOG_XTMC}")
    private String xtmc;

    Logger logger = LoggerFactory.getLogger(ServerConfiguration.class);

    @Value("${threadPool.event.corePoolSize:10}")
    private int eventThreadPoolCoreSize;

    @Value("${threadPool.event.maxPoolSize:30}")
    private int eventThreadPoolMaxSize;

    @Value("${threadPool.event.keepAliveTimeMs:60000}")
    private long eventThreadPoolKeepAliveTimeMs;

    @Value("${threadPool.event.workQueueSize:1000}")
    private int eventThreadPoolWorkQueueSize;

    @Value("${keda.address}")
    String kedaAddress;

    @Value("${keda.username}")
    String kedaUsername;

    @Value("${keda.password}")
    String kedaPassword;

    @Bean(name = "eventThreadPool")
    public ExecutorService eventExecuteService() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("event-thread-pool-%d").build();
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(eventThreadPoolCoreSize, eventThreadPoolMaxSize, eventThreadPoolKeepAliveTimeMs, TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>(eventThreadPoolWorkQueueSize), threadFactory, new ThreadPoolExecutor.AbortPolicy());
        return threadPoolExecutor;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        //加密器
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuditLogService getAuditLogService() {
        return new AuditLogServiceImpl(this.xtid, this.xtmc, this.zddz);
    }
//
//    @Bean
//    @Conditional(OperationSystemCondition.class)
//    public  KedaClient kedaClient() {
//        IPhoenixSDK_Linux sdk = new IPhoenixSDK_Linux();
//        int[] errorCode = new int[1];
//        int PlatBusMode = sdk.PlatTypeDetect(kedaAddress, errorCode);
//        boolean MouSelect = sdk.ModualSelect(PlatBusMode, 3, 1);
//       /* if(!sdk.Init()) {
//            logger.error("ServerConfiguration: sdk init failed. platBusMode={}", PlatBusMode);
//        }*/
//        return new KedaClient(kedaAddress, kedaUsername, kedaPassword, sdk);
//    }
//
//    @Bean
//    public AuditLogService getAuditLogService() {
//        return new AuditLogServiceImpl(this.xtid, this.xtmc, this.zddz);
//    }
}
